package com.irdstudio.efp.esb.service.impl.zx;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.irdstudio.basic.framework.core.exception.ESBException;
import com.irdstudio.efp.esb.common.constant.ESBTradeNoConstance;
import com.irdstudio.efp.esb.service.bo.req.BaseZXReqBean;
import com.irdstudio.efp.esb.service.bo.req.ShieldRiskHitQueryReports;
import com.irdstudio.efp.esb.service.bo.resp.zx.BaseZXResBean;
import com.irdstudio.efp.esb.service.bo.resp.zx.ShieldRiskHitQueryResponseReports;
import com.irdstudio.efp.esb.service.vo.zx.ZXVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
/**
 * 需求编号:2019D0519
 * 问题编号:
 * 开发人员: caoxin
 * 创建日期:2019/7/17
 * 功能描述:征信11007接口
 * 修改日期:2019/7/17
 * 修改描述:
 */
@Service("zx11007Service")
@DependsOn("ZXConfig")
public class ZX11007ServiceImpl extends BaseZXService<ShieldRiskHitQueryReports,ShieldRiskHitQueryResponseReports>{
    private static Logger logger = LoggerFactory.getLogger(ZX11007ServiceImpl.class);

    @Override
    protected void preProcess(BaseZXReqBean<ShieldRiskHitQueryReports> reqBean) {
        ShieldRiskHitQueryReports shieldRiskHitQueryReports = reqBean.getQryRptsInfArry().get(0);
        logger.debug("进入同盾11007规则命中查询接口，参数：" + Objects.toString(shieldRiskHitQueryReports));
    }

    @Override
    protected ZXVO postProcess(BaseZXReqBean<ShieldRiskHitQueryReports> reqBean, BaseZXResBean<ShieldRiskHitQueryResponseReports> resBean) throws Exception {
        Optional.
                ofNullable(resBean).
                map(i->i.getRepRptsInfArry()).
                map(i->i.get((0))).
                orElseThrow(()->new ESBException("同盾11007风险决策命中规则查询返回为空！"));
        logger.debug("同盾风险决策命中规则查询进行后续处理，请求参数: " + reqBean + ", 返回结果： " + resBean);
        return new ZXVO(Boolean.TRUE, resBean.getRepRptsInfArry());
    }

    @Override
    protected String tradeNo() {
        return ESBTradeNoConstance.ZX11007;
    }

    @Override
    protected String scene() {
        return "01";
    }

    @Override
    protected Function<String, BaseZXResBean<ShieldRiskHitQueryResponseReports>> converter() {
        return i-> JSONObject.parseObject(i,new TypeReference<BaseZXResBean<ShieldRiskHitQueryResponseReports>>(){});
    }
}
